function sandbox = fsb_recalc_onsets(sandbox,discard)

% FSB - DEV: Recalculate onset timing
%
% EXAMPLE:
% sandbox = fsb_recalc_onsets(sandbox)
%
% INPUT:
% sandbox: sandbox experiment struct
%
% OUTPUT:
% sandbox: sandbox experiment struct with updated onset information
% 
% CALLED BY:
% fsb_trialselect.m
% FSB.m
% 
%
% NOTES:
% in development
%
% $ Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~~~~~~~~~~~~
% Make sure discard is not zero
%~~~~~~~~~~~~~~~~~~~~~~

discard(discard<1) = [];

if max(discard)>length(sandbox.intrial);
    return
end

%~~~~~~~~~~~~~~~~~~~~~~
% Make sure that there are still onsets
%~~~~~~~~~~~~~~~~~~~~~~

if ~isfield(sandbox,'volmat') || ~isfield(sandbox.volmat,'evt_onsets');
return
end

%~~~~~~~~~~~~~~~~~~~~~~
% Get indices
%~~~~~~~~~~~~~~~~~~~~~~

allind = 1:length(sandbox.intrial);
allind(discard) = [];

%~~~~~~~~~~~~~~~~~~~~~~
% Look if onset information is complete
%~~~~~~~~~~~~~~~~~~~~~~
try
    onsets = sandbox.volmat.evt_onsets;
catch
    sandbox = fsb_volmat(sandbox);
    onsets = sandbox.volmat.evt_onsets;
end

%~~~~~~~~~~~~~~~~~~~~~~
% Get onset information out of cell
%~~~~~~~~~~~~~~~~~~~~~~

for ii = 1:size(onsets,2)
    onsets_n = onsets{1,ii};

    onsets2 = floor(onsets_n);
    onsets3 = onsets_n-onsets2;

    for k = 1:size(onsets_n,1)
        try
        onsets2(k) = find(onsets2(k)==allind);
        onsets_n(k) = onsets2(k)+onsets3(k);
        catch
            disp('Onset deleted')
        end
    end

    %~~~~~~~~~~~~~~~~~~~~~~
    % reinsert onset information into cell
    %~~~~~~~~~~~~~~~~~~~~~~

    onsets{1,ii} = onsets_n;
end

sandbox.volmat.evt_onsets = onsets;

end